/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.coreGenerator.base.entitygen.validation;


import com.inspur.edp.caf.generator.BaseGenerator;
import com.inspur.edp.caf.generator.composite.CompositeGenerator;
import com.inspur.edp.caf.generator.composite.CompositeGeneratorContext;
import com.inspur.edp.cef.designtime.api.IGspCommonField;
import com.inspur.edp.cef.designtime.api.collection.CommonValCollection;
import com.inspur.edp.cef.designtime.api.operation.CommonValidation;
import com.inspur.edp.cef.generator.coreGenerator.base.entitygen.DataTypeImpContext;

public abstract class DataTypeValidationsGenerator extends CompositeGenerator {
  private DataTypeImpContext dataTypeImplGenCtx;

  public DataTypeValidationsGenerator(DataTypeImpContext dataTypeImplGenCtx) {
    this.dataTypeImplGenCtx = dataTypeImplGenCtx;
  }


  // keyword:
  // ORIGINAL LINE: protected new DataTypeValidationsGenCtx getContext() { return
  // (DataTypeValidationsGenCtx)super.getContext(); }
  @Override
  protected DataTypeValidationsGenCtx getContext() {
    return (DataTypeValidationsGenCtx) super.getContext();
  }

  @Override
  protected CompositeGeneratorContext createCompositeContext() {
    DataTypeValidationsGenCtx valsGenCtx = new DataTypeValidationsGenCtx(dataTypeImplGenCtx);
    dataTypeImplGenCtx.setValsGenCtx(valsGenCtx);
    return valsGenCtx;
  }

  @Override
  protected final java.util.ArrayList<BaseGenerator> createCompositeItemGenerators() {
    java.util.ArrayList<BaseGenerator> list = new java.util.ArrayList<BaseGenerator>();
    addAfterModifyValGenerators(list);
    addBeforeSaveValGenerators(list);
    getNestedDtmGens(list);
    list.addAll(createExtendCompItemGenerators());
    return list;
  }


  /// #region NestedDtmGens
  private void getNestedDtmGens(java.util.ArrayList<BaseGenerator> list) {
    if (getContext().getDataType().getContainElements() == null) {
      return;
    }

    for (IGspCommonField item : getContext().getDataType().getContainElements()) {
      if (!item.getIsUdt()) {
        continue;
      }
    }
  }

  //endregion

  protected java.util.ArrayList<BaseGenerator> createExtendCompItemGenerators() {
    return new java.util.ArrayList<BaseGenerator>();
  }

  private void addAfterModifyValGenerators(java.util.ArrayList<BaseGenerator> list) {
    if (dataTypeImplGenCtx.getDataType().getValAfterModify() == null
        || dataTypeImplGenCtx.getDataType().getValAfterModify().size() == 0) {
      return;
    }

    for (CommonValidation item : dataTypeImplGenCtx.getDataType().getValAfterModify()) {
      list.add(getAfterModifyValGenrator(item));
    }
    CommonValCollection extendValidations = getExtendAfterModifyValidations();
    if (extendValidations != null && extendValidations.size() != 0) {

      for (CommonValidation item : extendValidations) {
        list.add(getAfterModifyValGenrator(item));
      }
    }
  }

  protected CommonValCollection getExtendAfterModifyValidations() {
    return null;
  }

  protected CommonValCollection getExtendBeforeSaveValidations() {
    return null;
  }

  protected abstract com.inspur.edp.cef.generator.coreGenerator.base.entitygen.validation.base.AfterModifyValidationGenerator getAfterModifyValGenrator(
      CommonValidation validation);

  protected abstract com.inspur.edp.cef.generator.coreGenerator.base.entitygen.validation.base.BeforeSaveValidationGenerator getBeforeSaveValGenrator(
      CommonValidation validation);

  private void addBeforeSaveValGenerators(java.util.ArrayList<BaseGenerator> list) {
    if (dataTypeImplGenCtx.getDataType().getValBeforeSave() == null
        || dataTypeImplGenCtx.getDataType().getValBeforeSave().size() == 0) {
      return;
    }

    for (CommonValidation item : dataTypeImplGenCtx.getDataType().getValBeforeSave()) {
      list.add(getBeforeSaveValGenrator(item));
    }
  }
}
